package com.raycloud.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.raycloud.manager.msgqueue.IMsgQueueHandler;
import com.raycloud.thread.MsgQueueThread;

public class ThreadTaskListener implements ServletContextListener{
	private static final Logger logger = Logger.getLogger(ThreadTaskListener.class);

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		
	}

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		ServletContext sc = sce.getServletContext();
		WebApplicationContext beanFactory = WebApplicationContextUtils.getWebApplicationContext(sc);
		IMsgQueueHandler msgQueueHandler = (IMsgQueueHandler) beanFactory.getBean("msgQueueHandler");
		//启动消息队列线程
		MsgQueueThread msgQueueThread = new MsgQueueThread();
		msgQueueThread.setMsgQueueHandler(msgQueueHandler);
		int threadCount = 4;
		for(int i = 0; i < threadCount; i++) {
			new Thread(msgQueueThread).start();
		}
		logger.info("消息队列线程 启动, 线程数是" + threadCount);
	}

}
